import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.InvalidClaimException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.pojo.VerifyUser;
import com.util.TokenUtil;
import io.jsonwebtoken.Claims;
import org.apache.ibatis.annotations.ResultType;
import org.junit.Test;

import java.util.Calendar;

public class test {
    final String key = "123abc";
    /**获取Token
     * @description: 测试java-jwt生成token字符串
     * @author: gsz
     * @date: 2022/3/12 10:36
     */
    @Test
    public void test1() {
        // 设置过期时长
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND,120);
        JWTCreator.Builder builder = JWT.create()
                //payload内容，由多个Claim组成
                .withClaim("userId", 1L)
                .withClaim("userName", "公孙瓒")
                .withExpiresAt(calendar.getTime());
        String token = builder.sign(Algorithm.HMAC256(key));
        System.out.println(token);
    }
    @Test
    public void textVerify(){
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyTmFtZSI6IuWFrOWtmeeTkiIsImV4cCI6MTY1Nzk1OTUxMSwidXNlcklkIjoxfQ.9dUc1pGVZTOhkoeHLljDaHfpemHthCY2yie6bxn-New";
        DecodedJWT verify = null;
        try {
            verify = JWT.require(Algorithm.HMAC256(key)).build().verify(token);
        } catch (SignatureVerificationException e){

            System.out.println(("--> 签名不一致 <--"));
        } catch (TokenExpiredException e){

            System.out.println(("--> 令牌过期 <--"));
        } catch (AlgorithmMismatchException e){

            System.out.println(("--> 签名算法不匹配 <--"));
        } catch (InvalidClaimException e){

            System.out.println(("--> payload不可用 <--"));
        } catch (Exception e){

            System.out.println(("--> 校验失败 <--"));
        }

        if (verify != null){
            // 获取payload信息，类型不匹配获取不到
            Long userId = verify.getClaim("userId").asLong();
            String userName = verify.getClaim("userName").asString();
            System.out.println(userId+" "+userName);
        }
    }
    @Test
    public void test9(){
        VerifyUser verifyUser = new VerifyUser("user1", "123456");
        String token = TokenUtil.getToken(verifyUser);
        System.out.println(token);
    }
    @Test
    public void test8(){
        VerifyUser verifyUser1=new VerifyUser("user2","123");
        String token2=TokenUtil.getToken(verifyUser1);
        System.out.println(token2);
    }
    @Test
    public void test7(){
        if(TokenUtil.verify("eyJhbGciOiJIUzI1NiJ9.eyJ1c2VySW5mbyI6Int9IiwiZXhwIjoxNjU3OTY0OTE5fQ.LQo6shrSWXP-Oc6WHreadOmUM9NXG90QIPrf_KrHHe8"))
        {
            System.out.println("验证成功");
        }else{
            System.out.println("验证失败");
        }
    }
    @Test
     public void test80(){
        int a=5,b=5;
        System.out.println("a+b="+a+b);
//        System.out.println("a-b="+a-b);
        System.out.println("a*b="+a*b);
        System.out.println("a/i1="+a/b);
    }

}
abstract class Base{
    public static void show() {

    }
}
class sub extends Base{

  public static void show(){
  }
}
